package com.jbookstore.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.jbookstore.dto.BookDto;
import com.jbookstore.dto.CategoryDto;
import com.jbookstore.service.BookService;
import com.jbookstore.service.CategoryService;

@Controller
public class MainController extends CommonController {

	@Autowired
	private CategoryService categoryService;
	@Autowired
	private BookService bookService;

	@RequestMapping(value = "main.do", method = RequestMethod.GET)
	public ModelAndView showMain(
			@RequestParam(value = "categoryId", required = false) String categoryId) {
		
		ModelAndView mav = new ModelAndView();

		if (categoryId == null)
			categoryId = "";

		// 카테고리 리스트를 가져온다.
		List<CategoryDto> categoryList = categoryService.getCategroyList();
		mav.addObject("categoryList", categoryList);

		// 북리스트를 가져온다.
		List<BookDto> bookList = bookService.getBookList(categoryId);
		mav.addObject("bookList", bookList);
		
		logger.info(bookList);
			
		mav.setViewName("/main/main");
		
		return mav;
	}
}
